package com.think.archive.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.think.archive.mapper.SysFuncMapper;
import com.think.archive.mapper.SysFuncMapper;
import com.think.archive.mapper.SysDeptMapper;
import com.think.archive.model.SysFunc;
import com.think.archive.model.SysDept;
import com.think.archive.model.SysDeptExample;
import com.think.archive.service.SysDeptService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    SysDeptMapper deptMapper;




    @Override
    public SysDept select(int id){
        SysDept dept=deptMapper.selectByPrimaryKey(id);
        return dept;
    }

    @Override
    public SysDept selectByDeptCode(String deptCode) {
        SysDeptExample example=new SysDeptExample();
        example.createCriteria().andDeptCodeEqualTo(deptCode);
        List<SysDept> depts=deptMapper.selectByExample(example);
        if(depts.isEmpty()){
            return null;
        }else{
            return depts.get(0);
        }
    }

    @Override
    public int insertOrUpdateDept(SysDept dept) {
        SysDept deptSave=deptMapper.selectByPrimaryKey(dept.getId());
        if(deptSave==null){
            dept.setCreater("system");
            dept.setCreateTime(new Date());
            return deptMapper.insert(dept);
        }
        else{
            deptSave.setDeptName(dept.getDeptName());
            deptSave.setStatus(dept.getStatus());
            deptSave.setSort(dept.getSort());
            deptSave.setRemark(dept.getRemark());
            return  deptMapper.updateByPrimaryKey(deptSave);
        }
    }

    @Override
    public int deleteDept(int id) {
        return deptMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int changeStatusDept(int id, String status) {
        SysDept dept=deptMapper.selectByPrimaryKey(id);
        dept.setStatus(status);
        return deptMapper.updateByPrimaryKey(dept);
    }

    @Override
    public JSONArray findDeptTree(String deptCode) {
        JSONArray jsonArray=findNextTree("",deptCode);
        return jsonArray;
    }

    @Override
    public List<SysDept> findNextLevelDept(String deptCode) {
        SysDeptExample example=new SysDeptExample();
        SysDeptExample.Criteria criteria=example.createCriteria();
        criteria.andDeptCodeLike(deptCode+"__");
        List<SysDept> depts=deptMapper.selectByExample(example);
        return depts;
    }

    public JSONArray findNextTree(String nodeCode,String deptCode){
        JSONArray leaf=new JSONArray();
        List<SysDept> depts=findNextLevelDept(nodeCode);
        for(SysDept dept:depts){
            JSONObject node=new JSONObject();
            node.put("text",dept.getDeptName());
            node.put("code",dept.getDeptCode());
            node.put("href","/admin/dept/list?deptCode="+dept.getDeptCode());
            JSONObject state=new JSONObject();
            JSONArray jsonArray=findNextTree(dept.getDeptCode(),deptCode);
            if(deptCode.startsWith(dept.getDeptCode())){
                state.put("expanded",true);
            }else{
                state.put("expanded",false);
            }
            if(deptCode.equals(dept.getDeptCode())){
                state.put("selected",true);
            }else{
                state.put("selected",false);
            }
            node.put("state",state);
            if(jsonArray.size()>0){
                node.put("nodes",jsonArray);
            }
            leaf.add(node);
        }
        return leaf;
    }
}
